package com.kyc.kycxkplaygame.greendao.manager;

import android.util.Log;
import com.kyc.kycxkplaygame.PlayGameApplication;
import com.kyc.kycxkplaygame.greendao.LevelPassBean;
import com.kyc.kycxkplaygame.greendao.gen.LevelPassBeanDao;

import java.util.List;

/**
 * Created by Administrator on 2017/12/21.
 */

public class LevelInfoManager {
    public static LevelInfoManager mHelp;
    private LevelPassBeanDao mDao;

    public static LevelInfoManager getInstance() {
        if (mHelp == null) {
            mHelp = new LevelInfoManager();
            return mHelp;
        } else {
            return mHelp;
        }
    }

    private LevelInfoManager() {
        mDao = PlayGameApplication.getInstance().getDaoSession().getLevelPassBeanDao();
    }

    public void deleteUserInfo(LevelPassBean entity) {
        mDao.delete(entity);
    }

    public void insertUserInfo(LevelPassBean entity) {
        mDao.insertOrReplace(entity);
    }


    public void insertLevleInfo(LevelPassBean entity) {
        mDao.insert(entity);
    }

    public void updateUserInfo(LevelPassBean entity) {
        mDao.update(entity);
    }

    public void insertOrUpdate(LevelPassBean entity) {
        List<LevelPassBean> infoLevelname = getInfoLevelname(entity.getLevel());
        LevelPassBean levelPassBean = new LevelPassBean();
        if (infoLevelname.size() > 0) {
            Log.i("KYCC", "修改？？？");
            levelPassBean.setId(infoLevelname.get(0).getId());
            levelPassBean.setLevel(infoLevelname.get(0).getLevel());
            levelPassBean.setStatus(infoLevelname.get(0).getStatus());
            Log.i("KYCC", "修改oldstarCount  ===  " + infoLevelname.get(0).getStarCount() + " ,  newstarCount === " + entity.getStarCount());
            if (infoLevelname.get(0).getStarCount() >= entity.getStarCount()) {
                levelPassBean.setStarCount(infoLevelname.get(0).getStarCount());
            } else {
                levelPassBean.setStarCount(entity.getStarCount());
            }

            if (infoLevelname.get(0).getLianjicount() >= entity.getLianjicount()) {
                levelPassBean.setLianjicount(infoLevelname.get(0).getLianjicount());
            } else {
                levelPassBean.setLianjicount(entity.getLianjicount());
            }

            if (infoLevelname.get(0).getPassfraction() >= entity.getPassfraction()) {
                levelPassBean.setPassfraction(infoLevelname.get(0).getPassfraction());
            } else {
                levelPassBean.setPassfraction(entity.getPassfraction());
            }

            if (infoLevelname.get(0).getPutaoNum() >= entity.getPutaoNum()) {
                levelPassBean.setPutaoNum(infoLevelname.get(0).getPutaoNum());
            } else {
                levelPassBean.setPutaoNum(entity.getPutaoNum());
            }
            mDao.update(levelPassBean);
        } else {
            Log.i("KYCC", "新增??????");
            mDao.insert(entity);
        }
    }

    public List<LevelPassBean> getUserInfo() {  //page 分页0开始每页15条
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        return mlist;
    }

    /*public List<UserInfoGdBean> getOperatorInfoUser(int page){  //page 分页0开始每页15条
        List<UserInfoGdBean> mlist = mDao.queryBuilder().where(OperatorInfoBeanDao.Properties.Type.eq(0))
                .orderDesc(OperatorInfoBeanDao.Properties.CreateDate)
                .offset(15*page)
                .limit(20)
                .list();
        return mlist;
    }*/

    public List<LevelPassBean> getInfoLevelAndPropname(String level) {  //page 分页0开始每页15条
        List<LevelPassBean> mlist = mDao.queryBuilder().where(LevelPassBeanDao.Properties.Level.eq(level)).list();
        return mlist;
    }

    public List<LevelPassBean> getInfoLevelname(String level) {  //page 分页0开始每页15条
        List<LevelPassBean> mlist = mDao.queryBuilder().where(LevelPassBeanDao.Properties.Level.eq(level)).list();
        return mlist;
    }


   /* public List<LevelPassBean> getInfoUserProp(String  propName){  //page 分页0开始每页15条
        List<LevelPassBean> mlist = mDao.queryBuilder().where(LevelPassBeanDao.Properties.PropName.eq(propName)).list();
        return mlist;
    }*/

    public int getLevelInfoCount() {  //page 分页0开始每页10条
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        return mlist.size();
    }

    public int getLevelCountStart() {
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        int count = 0;
        if (mlist.size() > 0) {
            for (LevelPassBean levelbean : mlist) {
                count += levelbean.getStarCount();
            }
        }
        return count;
    }

    public int getLevelThreeStart() {
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        int count = 0;
        if (mlist.size() > 0) {
            for (LevelPassBean levelbean : mlist) {
                if(levelbean.getStarCount() == 3) {
                    count += 1;
                }
            }
        }
        return count;
    }

    public int getLeveltwoStart() {
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        int count = 0;
        if (mlist.size() > 0) {
            for (LevelPassBean levelbean : mlist) {
                if(levelbean.getStarCount() == 2) {
                    count += 1;
                }
            }
        }
        return count;
    }

    public int getLevelOneStart() {
        List<LevelPassBean> mlist = mDao.queryBuilder().list();
        int count = 0;
        if (mlist.size() > 0) {
            for (LevelPassBean levelbean : mlist) {
                if(levelbean.getStarCount() == 1) {
                    count += 1;
                }
            }
        }
        return count;
    }


    /*public List<GuestRegistrationBean> getLobbyCompany(String name,String sex){  //page 分页0开始每页15条
        List<GuestRegistrationBean> mlist = mDao.queryBuilder()
                .where(GuestRegistrationBeanDao.Properties.Username.eq(name),
                        GuestRegistrationBeanDao.Properties.Gender.eq(sex))
                .orderDesc(GuestRegistrationBeanDao.Properties.CreateDate).list(); //.orderDesc(VisitorMsgEntityDao.Properties.Start_time).list();
        List<GuestRegistrationBean> mData = new ArrayList<>();
        if(mlist != null && mlist.size() > 0) {
            for (GuestRegistrationBean visit:mlist) {
                String createAt = visit.getCreateDate();
                Date date1 = DateUtil.stringToDate(createAt, "yyyy-MM-dd");
                //long aLong = Long.parseLong(createAt);
                long timeMillis = System.currentTimeMillis();
                Date date = DateUtil.longToDate(timeMillis, "yyyy-MM-dd");
                int i = date1.compareTo(date);
                if(i <= 0){
                    mData.add(visit);
                }
            }
        }
        return mData;
    }*/


}
